#include "ad_caiwu.h"
#include "ui_ad_caiwu.h"
#include "dlcw.h"
#include "admin_0.h"
#include "value.h"
#include "guoshu.h"
#include "ad_caiwu_del.h"
#include "ad_serchgood.h"
#include <QStandardItem>
#include <QMessageBox>

ad_CaiWu::ad_CaiWu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ad_CaiWu)
{
    ui->setupUi(this);
    setWindowTitle("财务系统");
}

ad_CaiWu::~ad_CaiWu()
{
    delete ui;
}

void ad_CaiWu::on_action_triggered()//退出系统
{
    this->close();
    Admin_0 *ad=new Admin_0;
    ad->show();
}

void ad_CaiWu::on_action_2_triggered()//历史进货
{
    QStringList headlist;
    headlist<<"进货时间"<<"支出金额"<<"进货产品";
    themodel=new QStandardItemModel(count2,3,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    FILE* fp;              //文件指针
    int n = 0;
    int i = 12;
    IAEP head = NULL;
    IAEP mp, p2, pr = NULL;
    fp = fopen("admin123_purchase", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        mp = (IAEP)malloc(M);   //向内存申请一段空间
        fread(mp, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = mp;
            p2 = mp;
            head->pre = NULL;
        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            mp->pre = p2;
            p2->next = mp;
            p2 = mp;
        }
    }
    int num=0;
    fclose(fp);             //关闭fp所指向的文件
    //printf("时     间          金   额                 收入来源或支出目的\n");
    QStandardItem *aItem;
    if (pr != NULL)
    {
        pr->next = NULL;
        mp = pr;

        while (mp != NULL)
        {
            //printf("%d.%d.%-12d%c%-24f%s\n", p->year, p->month, p->day, p->ch, p->iae, p->source_or_aim);
            aItem=new QStandardItem(QString::asprintf("%d.%d.%d",mp->year, mp->month, mp->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,0,aItem);
            aItem=new QStandardItem(QString::asprintf("%c%f",mp->ch, mp->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,aItem);
            aItem=new QStandardItem(QString::asprintf("%s",mp->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,aItem);
            i++;
            mp = mp->pre;
            num++;
        }
    }
    else if (pr == NULL)
    {
        //printf("您还没记录任何信息哦~");
        aItem=new QStandardItem(QString::asprintf("未记录任何信息"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
    }
}

void ad_CaiWu::on_action_3_triggered()//历史卖出
{
    QStringList headlist;
    headlist<<"产品编号"<<"产品"<<"产品形式"<<"卖出数量"<<"售价"<<"进货日期"<<"售出日期"<<"获得利润";
    themodel=new QStandardItemModel(count2+1,8,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStandardItem *aItem;
    if (count3 == 0)
    {
       //printf("您还没记录任何信息哦~");
       aItem=new QStandardItem(QString::asprintf("未记录任何信息"));
       aItem->setTextAlignment(Qt::AlignHCenter);
       themodel->setItem(0,1,aItem);
       return;
    }
    struct good *tmp;
    tmp=ss2;
    for(int i=0;i<count3;i++){
        //产品编号
        aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,0,aItem);
        //"产品"
        aItem=new QStandardItem(QString::asprintf("%s(%s)",tmp->name,tmp->nickname));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,1,aItem);
        //产品形式
        std::string selltyp;
        selltyp="盒装";
        if(tmp->typ==1) selltyp="散装";
        aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,2,aItem);
        //"卖出数量"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,3,aItem);
        //"售价"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->outprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,4,aItem);
        //"进货日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->indate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,5,aItem);
        //"售出日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->purchasedate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,6,aItem);
        //"获得利润"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->inprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,7,aItem);
        tmp=tmp->next;
    }
    tmp=NULL;

}
void ad_CaiWu::on_action_4_triggered()//总流水
{
    QStringList headlist;
    headlist<<"时间"<<"金额"<<"收入来源或支出目的";
    themodel=new QStandardItemModel(js()+2,3,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    FILE* fp;              //文件指针
    int n = 0;
    int i = 12;
    float sum = 0, sum1 = 0, sum2 = 0;
    IAEP head = NULL;
    IAEP p, p2, pr = NULL;
    fp = fopen("admin123", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
        return;
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        p = (IAEP)malloc(M);   //向内存申请一段空间
        fread(p, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = p;
            p2 = p;
            head->pre = NULL;

        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            p->pre = p2;
            p2->next = p;
            p2 = p;
        }
    }
    int num=0;
    fclose(fp);             //关闭fp所指向的文件
    //printf("时     间          金   额                 收入来源或支出目的\n");
    QStandardItem *aItem;
    if (pr != NULL)
    {
        pr->next = NULL;
        p = pr;

        while (p != NULL)
        {
            //printf("%d.%d.%-12d%c%-24f%s\n", p->year, p->month, p->day, p->ch, p->iae, p->source_or_aim);
            aItem=new QStandardItem(QString::asprintf("%d.%d.%d",p->year, p->month, p->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,0,aItem);
            aItem=new QStandardItem(QString::asprintf("%c%f",p->ch, p->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,aItem);
            aItem=new QStandardItem(QString::asprintf("%s",p->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,aItem);
            if (p->ch == '+')
            {
                sum1 += p->iae;
            }
            else if (p->ch == '-')
            {
                sum2 -= p->iae;
            }
            sum = sum1 + sum2;
            i++;
            p = p->pre;
            num++;
        }
        aItem=new QStandardItem(QString::asprintf("总结余"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("总支出"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("销售额"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
        num++;
        //printf("总   计： %f    总支出： %f    总收入： %f", sum, sum2, sum1);
        aItem=new QStandardItem(QString::asprintf("%f",sum));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum2));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum1));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
    }
    else if (pr == NULL)
    {
        //printf("您还没记录任何信息哦~");
        aItem=new QStandardItem(QString::asprintf("未记录任何信息"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
    }
}

int ad_CaiWu::js()//查看账单数量
{
    FILE* fp;              //文件指针
    int n = 0;
    IAEP xp;
    fp = fopen("admin123", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       return 0;
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {
        xp=(IAEP)malloc(M);//则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        fread(xp, M, 1, fp);
        n++;
        free(xp);
    }
    fclose(fp);
    return n;
}

void ad_CaiWu::on_action_5_triggered()//查看历史过期产品
{
    QStringList headlist;
    headlist<<"产品编号"<<"产品"<<"产品形式"<<"过期数量"<<"亏损金额"<<"过期时间";//indate过期日期
    themodel=new QStandardItemModel(count4+2,6,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStandardItem *aItem;
    int num=0;
    double sum_decr=0;//亏损总额
    if(count4==0){
        aItem=new QStandardItem(QString::asprintf("无过期产品"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        return;
    }
    struct good *tmp;
    tmp=ss3;
    for(int i=0;i<count4;i++){
        //产品编号
        aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,0,aItem);
        //"产品"
        aItem=new QStandardItem(QString::asprintf("%s(%s)",tmp->name,tmp->nickname));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,1,aItem);
        //产品形式
        std::string selltyp;
        selltyp="盒装";
        if(tmp->typ==1) selltyp="散装";
        aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,2,aItem);
        //"过期数量"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,3,aItem);
        //"亏损金额"
        sum_decr+=tmp->inprice*tmp->count;
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->inprice*tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,4,aItem);
        //过期时间
        aItem=new QStandardItem(QString::asprintf("%s",tmp->indate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,5,aItem);
        tmp=tmp->next;
    }
    tmp=NULL;
    aItem=new QStandardItem(QString::asprintf("总过期产品数目"));
    aItem->setTextAlignment(Qt::AlignHCenter);
    themodel->setItem(count4,0,aItem);
    aItem=new QStandardItem(QString::asprintf("总亏损金额"));
    aItem->setTextAlignment(Qt::AlignHCenter);
    themodel->setItem(count4,1,aItem);
    aItem=new QStandardItem(QString::asprintf("%d",count4));
    aItem->setTextAlignment(Qt::AlignHCenter);
    themodel->setItem(count4+1,0,aItem);
    aItem=new QStandardItem(QString::asprintf("%.2lf",sum_decr));
    aItem->setTextAlignment(Qt::AlignHCenter);
    themodel->setItem(count4+1,1,aItem);

}

void ad_CaiWu::on_action_6_triggered()//修改账单
{
    this->close();
    ad_caiwu_del *cwdl=new ad_caiwu_del;
    cwdl->show();
}

void ad_CaiWu::on_action_7_triggered()//查找商品
{
    ad_serchgood *as=new ad_serchgood(this);
    as->show();
}
